﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WPF_SFrost
{
    public partial class FormGroupDetail : Form
    {
        SqlConnection conn = new SqlConnection(SqlDbHelper.ConnectionString);
        SqlCommand cmd;
        int id;
        public FormGroupDetail(int id)
        {
            this.id = id;
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void FormGroupDetail_Load(object sender, EventArgs e)
        {
            txtId.Text = id.ToString();
            cmd = new SqlCommand("GetGroupById",conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter pGroupName = cmd.Parameters.Add("@groupname", SqlDbType.VarChar, 50);
            pGroupName.Direction = ParameterDirection.Output;
            SqlParameter pMemo = cmd.Parameters.Add("@memo", SqlDbType.VarChar, 200);
            pMemo.Direction = ParameterDirection.Output;
            SqlParameter pReturn = new SqlParameter("@return", SqlDbType.Int);
            pReturn.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(pReturn);
            cmd.Parameters.AddWithValue("@id", id);
            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            if ((int)pReturn.Value == 0)
            {
                txtName.Text = pGroupName.Value.ToString();
                txtMemo.Text = pMemo.Value.ToString();
            }
            else
            {
                MessageBox.Show("查询出错");
            }
            sdr.Close();
            conn.Close();
        }



        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckModifyName(txtName.Text.Trim()))
                return;
            string sql = "update contactgroup set groupname = @groupname,memo = @memo where id = @id";
            SqlParameter[] parameters =
            {
                new SqlParameter("@groupname",SqlDbType.VarChar,50),
                new SqlParameter("@memo",SqlDbType.VarChar,200),
                new SqlParameter("@id",SqlDbType.Int)
            };
            parameters[0].Value = txtName.Text.Trim();
            parameters[1].Value = txtMemo.Text.Trim();
            parameters[2].Value = Convert.ToInt32(txtId.Text.Trim());
            int n = SqlDbHelper.ExecuteNonQuery(sql, CommandType.Text, parameters);
            if (n == 1)
            {
                MessageBox.Show("修改成功。");
            }
            else
            {
                MessageBox.Show("更新失败。");
            }
            this.Close();
        }
        bool CheckModifyName(string groupName)
        {
            bool check = true;
            if (groupName == "") {
                MessageBox.Show("分类名称不能为空。");
                txtName.Focus();
                check = false;
            }
            else
            {
                string sql = "select count(*) from contactgroup where groupname = @groupname and id <> @id";
                SqlParameter[] parameters =
                {
                    new SqlParameter("@groupname",groupName),
                    new SqlParameter("@id",id)
                };
                int n = (int)SqlDbHelper.ExecuteScalar(sql, CommandType.Text, parameters);
                if (n > 0)
                {
                    MessageBox.Show("分组名称已存在。");
                    txtName.Focus();
                    check = false;
                }
            }
            conn.Close();
            return check;
        }
    }
}
